{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "otherwise-contractor",
   "metadata": {},
   "source": [
    "# Belgian IBAN Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "agricultural-remove",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lovely-security",
   "metadata": {},
   "source": [
    "The function `clean_be_iban()` cleans a column containing Belgian IBAN (International Bank Account Number) strings, and standardizes them in a given format. The function `validate_be_iban()` validates either a single Belgian IBAN strings, a column of Belgian IBAN strings or a DataFrame of Belgian IBAN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "human-saver",
   "metadata": {},
   "source": [
    "Belgian IBAN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"BE32123456789002\"\n",
    "* `standard`: Belgian IBAN strings with proper whitespace in the proper places, like \"BE32 1234 5678 9002\"\n",
    "* `bic`: return the BIC for the bank that this number refers to, like \"CTBKBEBX\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_be_iban()` and `validate_be_iban()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "scientific-briefs",
   "metadata": {},
   "source": [
    "### An example dataset containing Belgian IBAN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "atomic-istanbul",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"be_iban\": [\n",
    "            \"BE32 123-4567890-02\",\n",
    "            \"BE41091811735141\",             # incorrect national check digits\n",
    "            \"BE83138811735115\",             # unknown bank code\n",
    "            \"GR1601101050000010547023795\",  # not a Belgian IBAN\n",
    "            \"BE 48 3200 7018 4927\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interior-texas",
   "metadata": {},
   "source": [
    "## 1. Default `clean_be_iban`\n",
    "\n",
    "By default, `clean_be_iban` will clean be_iban strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "floral-globe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_be_iban\n",
    "clean_be_iban(df, column = \"be_iban\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "convertible-firewall",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "comfortable-latvia",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "corresponding-mirror",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "specified-minute",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, column = \"be_iban\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intermediate-stevens",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "demanding-oracle",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, column = \"be_iban\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "backed-irish",
   "metadata": {},
   "source": [
    "### `bic`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unauthorized-cardiff",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, column = \"be_iban\", output_format=\"bic\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "certain-darwin",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Belgian IBAN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "removed-bathroom",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, column=\"be_iban\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "characteristic-agent",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "educated-ottawa",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "practical-magnet",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, \"be_iban\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "union-martin",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "little-diving",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_be_iban(df, \"be_iban\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "impressive-stephen",
   "metadata": {},
   "source": [
    "## 4. `validate_be_iban()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "comparable-velvet",
   "metadata": {},
   "source": [
    "`validate_be_iban()` returns `True` when the input is a valid Belgian IBAN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_be_iban()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_be_iban()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_be_iban()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "precise-casino",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_be_iban\n",
    "print(validate_be_iban(\"BE32 123-4567890-02\"))\n",
    "print(validate_be_iban(\"BE41091811735141\"))\n",
    "print(validate_be_iban(\"BE83138811735115\"))\n",
    "print(validate_be_iban(\"GR1601101050000010547023795\"))\n",
    "print(validate_be_iban(\"004085616\"))\n",
    "print(validate_be_iban(\"hello\"))\n",
    "print(validate_be_iban(np.nan))\n",
    "print(validate_be_iban(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "applicable-catalog",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "every-diagnosis",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_be_iban(df[\"be_iban\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complete-murder",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "genetic-genius",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_be_iban(df, column=\"be_iban\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "filled-bulgaria",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "compound-sitting",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_be_iban(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "regulated-saying",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
